package org.eclipse.equinox.spring.osgi;
/**
 * This file is part of org.springframework.equinox.deployer. 
 * Copyright (C) olivier.evalet@genebio.com and genebio team
 * 
 * org.springframework.equinox.deployer is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * org.springframework.equinox.deployer is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * org.springframework.equinox.deployer. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Special thanks for Bernd Kolb the first author of this file 
 */

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.io.Resource;
import org.springframework.web.context.WebApplicationContext;

/**
 * Wraps a normal {@link ApplicationContext} into a
 * {@link WebApplicationContext}
 * 
 * 
 * @author Bernd Kolb
 * 
 */
public class WrappedApplicationContext implements WebApplicationContext {

	private final ApplicationContext appCtx;
	private ServletContext servletContext;

	/**
	 * @param appCtx
	 *            the {@link ApplicationContext} to be wrapped
	 * @param servletContext
	 *            the {@link ServletContext} for this
	 *            {@link WebApplicationContext}
	 */
	public WrappedApplicationContext(ApplicationContext appCtx,
			ServletContext servletContext) {
		this.appCtx = appCtx;
		this.servletContext = servletContext;
	}

	@Override
	public ServletContext getServletContext() {
		return servletContext;
	}

	public boolean containsBean(String name) {
		return appCtx.containsBean(name);
	}

	public boolean containsBeanDefinition(String beanName) {
		return appCtx.containsBeanDefinition(beanName);
	}

	public boolean containsLocalBean(String name) {
		return appCtx.containsLocalBean(name);
	}

	public String[] getAliases(String name) {
		return appCtx.getAliases(name);
	}

	public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
			throws IllegalStateException {
		return appCtx.getAutowireCapableBeanFactory();
	}

	public Object getBean(String name, Class requiredType)
			throws BeansException {
		return appCtx.getBean(name, requiredType);
	}

	public Object getBean(String name) throws BeansException {
		return appCtx.getBean(name);
	}
	
	public Object getBean(String name, Object[] args) throws BeansException {
		return appCtx.getBean(name,args);
	}

	public int getBeanDefinitionCount() {
		return appCtx.getBeanDefinitionCount();
	}

	public String[] getBeanDefinitionNames() {
		return appCtx.getBeanDefinitionNames();
	}

	public String[] getBeanNamesForType(Class type, boolean includePrototypes,
			boolean allowEagerInit) {
		return appCtx.getBeanNamesForType(type, includePrototypes,
				allowEagerInit);
	}

	public String[] getBeanNamesForType(Class type) {
		return appCtx.getBeanNamesForType(type);
	}

	public Map getBeansOfType(Class type, boolean includePrototypes,
			boolean allowEagerInit) throws BeansException {
		return appCtx.getBeansOfType(type, includePrototypes, allowEagerInit);
	}

	public Map getBeansOfType(Class type) throws BeansException {
		return appCtx.getBeansOfType(type);
	}

	public ClassLoader getClassLoader() {
		return appCtx.getClassLoader();
	}

	public String getDisplayName() {
		return appCtx.getDisplayName();
	}

	public String getMessage(MessageSourceResolvable resolvable, Locale locale)
			throws NoSuchMessageException {
		return appCtx.getMessage(resolvable, locale);
	}

	public String getMessage(String code, Object[] args, Locale locale)
			throws NoSuchMessageException {
		return appCtx.getMessage(code, args, locale);
	}

	public String getMessage(String code, Object[] args, String defaultMessage,
			Locale locale) {
		return appCtx.getMessage(code, args, defaultMessage, locale);
	}

	public ApplicationContext getParent() {
		return appCtx.getParent();
	}

	public BeanFactory getParentBeanFactory() {
		return appCtx.getParentBeanFactory();
	}

	public Resource getResource(String location) {
		return appCtx.getResource(location);
	}

	public Resource[] getResources(String locationPattern) throws IOException {
		return appCtx.getResources(locationPattern);
	}

	public long getStartupDate() {
		return appCtx.getStartupDate();
	}

	public Class getType(String name) throws NoSuchBeanDefinitionException {
		return appCtx.getType(name);
	}

	public boolean isPrototype(String name)
			throws NoSuchBeanDefinitionException {
		return appCtx.isPrototype(name);
	}

	public boolean isSingleton(String name)
			throws NoSuchBeanDefinitionException {
		return appCtx.isSingleton(name);
	}

	public boolean isTypeMatch(String name, Class targetType)
			throws NoSuchBeanDefinitionException {
		return appCtx.isTypeMatch(name, targetType);
	}

	public void publishEvent(ApplicationEvent event) {
		appCtx.publishEvent(event);
	}

	public String getId() {
		return appCtx.getId();
	}


}
